#include "Movie.h"

//default constructor
Movie::Movie()
{
	mName = "";
	mRating = UNRATED;
	mBudget = 0;
	mRevenue = 0;
}
//constructor
Movie::Movie( const string& name, Rating rating, int budget, int revenue )
{
	mName = name;
	mRating = rating;
	mBudget = budget;
	mRevenue = revenue;
	mReviewFileLoc = "";
}

Movie::Movie( const string& name, Rating rating, int budget, int revenue, string reviewFileLoc )
{
	mName = name;
	mRating = rating;
	mBudget = budget;
	mRevenue = revenue;
	mReviewFileLoc = reviewFileLoc;
	loadReviews();
}

string Movie::formatReport()
{
	stringstream sstream;
	float averageReview = getAverageReview();

	//Settings flags for sstream
	sstream.precision(2);
	sstream.setf(ios_base::fixed | ios_base::showpoint);

	sstream << left << setw(15) << mName << setw(10) << right << "Rating:" << left << getRatingName()
		    << setw(20) << right << "Total Profit:" << setw(15) << left << getTotalProfit() << setw(15) 
			<< right << "Average Stars: " << left << averageReview << setw(15) << right 
			<< "profit/star:" << left << getTotalProfit() / averageReview;

	return sstream.str();
}

//return the string which correlates with the rating
string Movie::getRatingName()
{
	stringstream sstream;

	sstream << RATING_WORDS[mRating];

	return sstream.str();
}

void Movie::loadReviews()
{
	if ( mReviewFileLoc == "" )
		return;
	else
	{
		int tempValue;
		fstream input;
		input.open("reviews\\" + mReviewFileLoc + ".txt");

		if ( input.good() )
		{
			while ( !input.eof() )
			{
				input >> tempValue;
				addReview(tempValue);
			}

			input.close();
		}
		else
		{
			cout << "Error occured while opening " << mReviewFileLoc << ".txt" << endl;
			input.close();
		}
	}
}

//add a review
void Movie::addReview( int numStars )
{
	if ( numStars <= 5 && numStars >= 0 )
	{
		mReviews.push_back(numStars);
	}
}

int Movie::getNumReviews( int numStars)
{
	int counter = 0;

	for ( unsigned int i = 0; i < mReviews.size(); i++)
	{
		if ( mReviews[i] == numStars)
			counter++;
	}

	return counter;
}

float Movie::getAverageReview()
{
	int totalRating = 0;

	if ( mReviews.size() == 0 )
	{
		cout << "No reviews found defaulting to -1.0" << endl;
		return -1.0;
	}

	for ( unsigned int i = 0; i < mReviews.size(); i++ )
	{
		totalRating += mReviews[i];
	}

	return (float)totalRating / mReviews.size();
}

ostream& operator<<( ostream& out, Movie& mov )
{
	out << mov.formatReport();
	return out;
}